package com.cts.c2;

import android.content.Intent;
import android.os.Bundle;
import android.support.v4.app.ActionBar;
import android.support.v4.app.FragmentActivity;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;

import com.cts.c2.services.LoginService;
import com.cts.c2.services.LoginServiceImpl;
import com.cts.c2.vo.AuthenticateVO;

public class LoginActivity extends FragmentActivity {
	private static final int SHOW_HOME = 1;

	/** Called when the activity is first created. */
	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.login);
		addLoginBtnListioner();
		ActionBar bar = getSupportActionBar();
		// bar.addTab(bar.newTab().setText("Tab 1").setTabListener(this));
	}

	private void addLoginBtnListioner() {
		Button btn = (Button) findViewById(R.id.loginBtn);
		btn.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View v) {
				AuthenticateVO authenticateVO = new AuthenticateVO();
				authenticateVO.setUserId(((EditText) findViewById(R.id.userId))
						.getText().toString());
				authenticateVO.setPassword(((EditText) findViewById(R.id.pwd))
						.getText().toString());

				LoginService loService = new LoginServiceImpl();
				if (loService.authenticateUser(authenticateVO)) {
					Toast.makeText(getApplicationContext(), "Logging in..",
							Toast.LENGTH_SHORT).show();
					Intent myIntent = new Intent();
					myIntent.setClassName("com.cts.c2",
							"com.cts.c2.HomeActivity");
					startActivityForResult(myIntent, SHOW_HOME);
				} else {
					Toast.makeText(getApplicationContext(), "Login Failed..",
							Toast.LENGTH_SHORT).show();
				}

			}
		});

	}

}
